package com.example.projectdemo.xfrv;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.example.projectdemo.R;
import com.example.projectdemo.rvrv.ApiService;
import com.example.projectdemo.rvrv.NavBean;
import com.example.projectdemo.rvrv.RightAdapter;
import com.example.projectdemo.xfrv.xf2.Xf2Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/***
 * 第一种方式
 * https://www.jianshu.com/p/c596f2e6f587
 * 点评：耦合高、代码有bug，建议略过
 * 实现思路：
 * 1.每一个RecyclerView的item的布局(下文叫itemUI)里面都包含“吸顶文本”这个布局(下文叫StickyLayout)，
 * 根据当前itemA和上一个itemB的吸顶信息是否相同，决定是否展示itemA的StickyLayout.
 * 2.包含RecyclerView的布局(下文叫wrapperUI)最上部分，有一个假的StickyLayout(下文叫FakeStickyLayout).
 * 3.监听RecyclerView的滚动，根据RecyclerView的滚动距离，决定FakeStickyLayout向上或者向下滚动的距离.
 *
 * 第二种方式：通过自定义decor来实现吸顶
 * 点评：
 * 实现思路：
 *
 *
 *
 */
public class XiFuRvActivity extends AppCompatActivity {

    private RecyclerView mRv;

    private RightAdapter mAdapter;
    private Map<Integer, String> mKeys;
    private List<NavBean.DataBean> mList;
    private FloatingItemDecoration mFloatingItemDecoration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xi_fu_rv);


        mRv = findViewById(R.id.xfrv);

        mList = new ArrayList<>();
        mKeys = new HashMap<>();
        loadData();

        mAdapter = new RightAdapter(this, mList);
        mRv.setAdapter(mAdapter);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRv.setLayoutManager(manager);

        mFloatingItemDecoration = new FloatingItemDecoration(this, Color.GRAY, 1, 1);
        mRv.addItemDecoration(mFloatingItemDecoration);
        mRv.setHasFixedSize(true);


        mAdapter.setOnItemClickListener(new RightAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), Xf2Activity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), XiFuRv1Activity.class);
                startActivity(intent);
            }
        });
    }

    private void loadData() {
        String url = "https://www.wanandroid.com/";
        //1、获取Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                //增加返回值为Json的支持
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2、然后通过动态代理获取到所定义的接口，获取到返回
        ApiService ipServiceForPath = retrofit.create(ApiService.class);
        Call<NavBean> call = ipServiceForPath.getNav();//1
        //3、异步请求
        //同步请求是call.execute()
        call.enqueue(new Callback<NavBean>() {
            @Override
            public void onResponse(Call<NavBean> call, Response<NavBean> response) {
                List<NavBean.DataBean> data = response.body().getData();
                for (int i = 0; i < data.size(); i++) {
                    //if (i % 2 == 0)//可以根据位置放K，想要显示的item分组
                    mKeys.put(i, data.get(i).getName());
                }
                mList.addAll(data);
                mFloatingItemDecoration.setKeys(mKeys);
//                mFloatingItemDecoration.setmTitleHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
                mFloatingItemDecoration.setmTitleHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics()));
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<NavBean> call, Throwable t) {

            }
        });
    }
}


